﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApp1.Theme
{
    public class HighDpiForm : Form
    {
        // DPI缩放因子 (96 DPI为基准)
        protected float DpiScale => DeviceDpi / 96f;

        // 当前DeviceDpi值记录
        protected int CurrentDpi { get; private set; } = 96;

        public HighDpiForm()
        {
            // 关键DPI配置
            AutoScaleMode = AutoScaleMode.Dpi;
            AutoScaleDimensions = new SizeF(96F, 96F);

            // 注册DPI变化事件
            DpiChanged += OnDpiChanged;

            // 工具条控件模糊修复
            foreach (var ctrl in FindControls<ToolStrip>())
            {
                ctrl.Renderer = new HighDpiToolStripRenderer();
            }
        }

        #region DPI事件处理
        private void OnDpiChanged(object sender, DpiChangedEventArgs e)
        {
            CurrentDpi = e.DeviceDpiOld;
            if (AutoScaleMode != AutoScaleMode.None)
            {
                Scale(new SizeF(
                    (float)e.DeviceDpiNew / e.DeviceDpiOld,
                    (float)e.DeviceDpiNew / e.DeviceDpiOld
                ));

                HandleDpiChange(e.DeviceDpiNew, e.DeviceDpiOld);
            }
        }
        #endregion

        #region 虚拟方法 - 供派生类实现
        protected virtual void HandleDpiChange(int newDpi, int oldDpi)
        {
            // 派生类可重写以执行自定义DPI处理
            // 示例: RebuildResourcesForNewDpi(newDpi);
        }

        protected virtual void ScaleChildren(float scaleFactor)
        {
            // 派生类可重写以实现自定义缩放逻辑
        }
        #endregion

        #region DPI辅助方法
        // 查找特定类型控件
        protected IEnumerable<T> FindControls<T>() where T : Control
        {
            var queue = new Queue<Control>();
            queue.Enqueue(this);

            while (queue.Count > 0)
            {
                var control = queue.Dequeue();
                if (control is T matched)
                    yield return matched;

                foreach (Control child in control.Controls)
                    queue.Enqueue(child);
            }
        }

        // 加载DPI适应性图像
        protected Image LoadScaledImage(Image baseImage)
        {
            return ScaleImage(baseImage, DpiScale);
        }

        // 图像缩放方法
        protected static Bitmap ScaleImage(Image image, float scaleFactor)
        {
            if (Math.Abs(scaleFactor - 1.0f) < 0.01f)
                return (Bitmap)image.Clone();

            var scaledSize = new Size(
                (int)Math.Round(image.Width * scaleFactor),
                (int)Math.Round(image.Height * scaleFactor)
            );

            var scaledBmp = new Bitmap(scaledSize.Width, scaledSize.Height);

            using (var g = Graphics.FromImage(scaledBmp))
            {
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                g.DrawImage(image, new Rectangle(Point.Empty, scaledSize));
            }

            return scaledBmp;
        }

        // 字体DPI适配
        protected Font ScaleFont(Font baseFont)
        {
            if (Math.Abs(DpiScale - 1.0f) < 0.01f)
                return baseFont;

            // Windows推荐字体缩放公式
            var emSize = baseFont.Size * DpiScale * 0.75f;
            return new Font(
                baseFont.FontFamily,
                emSize,
                baseFont.Style,
                baseFont.Unit,
                baseFont.GdiCharSet,
                baseFont.GdiVerticalFont
            );
        }
        #endregion
    }

    // 专门解决工具栏模糊问题的渲染器
    internal class HighDpiToolStripRenderer : ToolStripSystemRenderer
    {
        protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e)
        {
            e.TextFont = new Font(e.TextFont.FontFamily,
                                 e.TextFont.Size * e.ToolStrip.DeviceDpi / 96f,
                                 e.TextFont.Style);
            base.OnRenderItemText(e);
        }
    }
}
